﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/14 20:15:50
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 主题分类。
    /// </summary>
    public class Category : BaseObject
    {
        /// <summary>
        /// 初始化类<see cref="T:Mozlite.Components.Category"/>。
        /// </summary>
        public Category() {
            this.ApplicationType = ApplicationType.Common;
            this.SettingsID = SiteSettings.Current.SettingsID;
        }

        /// <summary>
        /// 从数据库中加载数据。
        /// </summary>
        /// <param name="reader">数据库读取器。</param>
        protected override void Load(System.Data.IDataReader reader)
        {
            this.SettingsID = reader.GetInt32("SettingsID");
            this.CategoryID = reader.GetInt32("CategoryID");
            this.ParentID = reader.GetInt32("ParentID");
            this.Key = reader.GetString("Key");
            this.CategoryName = reader.GetString("CategoryName");
            this.Description = reader.GetString("Description");
            this.CreateDate = reader.GetDateTime("CreateDate");
            this.LogoUrl = reader.GetString("LogoUrl");
            this.Threads = reader.GetInt32("Threads");
            this.ApplicationType = reader.TGet<ApplicationType>("ApplicationType");
        }

        /// <summary>
        /// 是否显示。
        /// </summary>
        public bool IsVisiable { get { return Get<bool>("IsVisiable", true); } set { Set("IsVisiable", value); } }

        /// <summary>
        /// 网站设置ID。
        /// </summary>
        public int SettingsID { get; set; }

        /// <summary>
        /// 分类ID。
        /// </summary>
        public int CategoryID { get; set; }

        /// <summary>
        /// 父分类ID。
        /// </summary>
        public int ParentID { get; set; }

        /// <summary>
        /// 分类键。
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 分类名称。
        /// </summary>
        public string CategoryName { get; set; }

        /// <summary>
        /// 分类描述。
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 创建时间。
        /// </summary>
        public DateTime CreateDate { get; set; }

        /// <summary>
        /// Logo地址。
        /// </summary>
        public string LogoUrl { get; set; }

        /// <summary>
        /// 主题数量。
        /// </summary>
        public int Threads { get; set; }

        /// <summary>
        /// 应用程序类型。
        /// </summary>
        public ApplicationType ApplicationType { get;set; }

        private List<Category> children;
        /// <summary>
        /// 获取子分类列表。
        /// </summary>
        public List<Category> Children {
            get {
                if (children == null)
                {
                    children = (from category in Categories.LoadCategories().Values
                                where category.ParentID == this.CategoryID
                                select category).ToList<Category>();
                }
                return children;
            }
        }

        private Category parent;
        /// <summary>
        /// 父分类。
        /// </summary>
        public Category Parent {
            get {
                if (parent == null)
                {
                    if (!Categories.LoadCategories().TryGetValue(this.ParentID, out parent))
                        parent = new Category();
                }
                return parent;
            }
        }

        /// <summary>
        /// 刷新父分类和子分类。
        /// </summary>
        public void Refresh()
        {
            children = null;
            parent = null;
        }

        private string categoryUrl;
        /// <summary>
        /// 获取文章访问地址。
        /// </summary>
        public string CategoryUrl
        {
            get
            {
                if (categoryUrl == null)
                {
                    if (string.IsNullOrEmpty(Key))
                        categoryUrl = Mozlite.Web.UI.RouteManager.FormatUrl(this.ApplicationType + "_categoriesviewbyid", new { id = this.CategoryID });
                    else
                        categoryUrl = Mozlite.Web.UI.RouteManager.FormatUrl(this.ApplicationType + "_categoriesviewbykey", new { key = this.Key });
                }
                return categoryUrl;
            }
        }
    }
}